﻿using PacoToolKit.Format;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Protocol.SouthGridProtocol.Blocks
{
    public class FileTransferBlock
    {
        /// <summary>
        /// 段号
        /// </summary>
        public int currentFileGroupIndex { get; set; }
        /// <summary>
        /// 段长度
        /// </summary>
        public int currentFileSize { get; set; }
        /// <summary>
        /// 段内容
        /// </summary>
        public string currentFileData { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="currentFileGroupIndex">段号</param>
        /// <param name="currentFileSize">段长度，单位：字节</param>
        /// <param name="currentFileData">段内容</param>
        public FileTransferBlock(int currentFileGroupIndex, int currentFileSize, string currentFileData)
        {
            this.currentFileGroupIndex = currentFileGroupIndex;
            this.currentFileSize = currentFileSize;
            this.currentFileData = currentFileData;
        }

        /// <summary>
        /// 数据块转换成报文
        /// </summary>
        /// <returns></returns>
        public string toString()
        {
            string blocks = Formater.ReverseHexString(Formater.DECtoHEX(currentFileGroupIndex.ToString(), 4)) +
                Formater.ReverseHexString(Formater.DECtoHEX(currentFileSize.ToString(), 4)) +
                currentFileData;//file data no reverse
            return Formater.BytesToHexStr(Formater.crc16(Formater.HexStringToBytesKeep(blocks)));
            //return blocks + "00";//00 is crc16
        }
    }
}
